package com.example.a28796.beautiful.styAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a28796.beautiful.R;
import com.example.a28796.beautiful.stybean.HotBean;
import com.squareup.picasso.Picasso;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by lenovo on 2016/8/30.
 */
public class HotAdapter extends BaseAdapter {
    private List<HotBean.ObjBean.ListDataBean> mData;
    private Context context;
    private ImageOptions options;
    public HotAdapter(List<HotBean.ObjBean.ListDataBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
        options = new ImageOptions.Builder().setCircular(true).setUseMemCache(true).build();
    }

    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        HotBean.ObjBean.ListDataBean dataBean = mData.get(position);
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_hot,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(dataBean.getTitle());
        holder.content.setText(dataBean.getSummary());
        holder.userName.setText(dataBean.getUser().getNickname());
        holder.pager.setText("共"+dataBean.getArticleGroupNum().getArticleNum()+"篇");
        holder.count.setText(dataBean.getArticleGroupNum().getViewNum()/10000+"万+浏览");
        x.image().bind(holder.userIcon,dataBean.getUser().getHeadUrl(),options);
        if(dataBean.getPhotoes()!=null&&dataBean.getPhotoes().size()!=0) {
            Picasso.with(context).load(dataBean.getPhotoes().get(0).getUrl()).into(holder.userPic);
        }

        return convertView;
    }
    public static class ViewHolder{
        private ImageView userIcon,userPic;
        private TextView title,content,userName,pager,count;
        public ViewHolder(View view) {
            userIcon = (ImageView) view.findViewById(R.id.iv_item_hot_icon);
            userPic = (ImageView) view.findViewById(R.id.iv_item_hot_show);
            title = (TextView) view.findViewById(R.id.tv_item_hot_title);
            content = (TextView) view.findViewById(R.id.tv_item_hot_content);
            userName = (TextView) view.findViewById(R.id.tv_item_hot_username);
            pager = (TextView) view.findViewById(R.id.tv_item_hot_pager);
            count = (TextView) view.findViewById(R.id.tv_item_hot_count);
        }
    }
}
